//
//  PhotoBrowerViewController.swift
//  WB
//
//  Created by leador on 17/5/18.
//  Copyright © 2017年 袁满. All rights reserved.
//

import UIKit
import SnapKit

private let photoBrowerCell = "photoBrowerCell"

class PhotoBrowerViewController: UIViewController {
    
    lazy var collectionView : UICollectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: PhotoBrowerCollectionViewFlowlayout())
    lazy var closeBtn : UIButton = UIButton(title: "关 闭", titleSize: 14, backgroundColor: UIColor.darkGray)
    lazy var saveBtn : UIButton = UIButton(title: "保 存", titleSize: 14, backgroundColor: UIColor.darkGray)
    
    var indexPath : NSIndexPath
    var picUrls : [URL] = [URL]()
    
    init(indexPath:NSIndexPath,picUrls:[URL]) {
        
        self.indexPath = indexPath
        self.picUrls = picUrls
        
        super.init(nibName: nil, bundle: nil)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        
        setupAction()
        
    }
    
}

extension PhotoBrowerViewController {
    
    func setupUI() {
        
        view.addSubview(collectionView)
        view.addSubview(closeBtn)
        view.addSubview(saveBtn)
        
        collectionView.frame = view.bounds
        collectionView.dataSource = self
        collectionView.register(PhotoBrowerCollectionViewCell.self, forCellWithReuseIdentifier: photoBrowerCell)
        
        closeBtn.snp.updateConstraints { (make) in
            
            make.left.equalTo(20)
            make.bottom.equalTo(-20)
            make.width.equalTo(90)
            make.height.equalTo(32)
            
        }
        
        saveBtn.snp.updateConstraints { (make) in
            
            make.right.equalTo(-20)
            make.bottom.equalTo(closeBtn.snp.bottom)
            make.width.equalTo(closeBtn.snp.width)
            make.height.equalTo(closeBtn.snp.height)
            
        }
        
    }
    
    func setupAction () {
        
        self.closeBtn.addTarget(self, action: #selector(closeBtnClick), for: .touchUpInside)
        self.saveBtn.addTarget(self, action: #selector(saveBtnClick), for: .touchUpInside)
        
    }
    
}

extension PhotoBrowerViewController {
    
    func closeBtnClick() {
        dismiss(animated: true, completion: nil)
    }
    
    func saveBtnClick() {
        print("saveBtnClick")
    }
    
}

extension PhotoBrowerViewController : UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return picUrls.count
        
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: photoBrowerCell, for: indexPath) as! PhotoBrowerCollectionViewCell
    
        cell.picUrl = picUrls[indexPath.row]
        
        return cell

    }
    
}

class PhotoBrowerCollectionViewFlowlayout : UICollectionViewFlowLayout {
    
    override func prepare() {
        
        super.prepare()
        itemSize = UIScreen.main.bounds.size
        minimumLineSpacing = 0
        minimumInteritemSpacing = 0
        scrollDirection = .horizontal
        
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.showsVerticalScrollIndicator = false
        collectionView?.isPagingEnabled = true
        
    }
    
}
